package porter

import (
	"context"
	"github.com/tidwall/gjson"
	"shop/src/model"
	"shop/src/open/ks"
	"time"
)

var ksClient = ks.NewClient()

const (
	tradeCursor  = "result.data.cursor"
	refundCursor = "result.data.pcursor"
	endCursor    = "nomore"
)

type ksTradeFetcher struct {
	store *model.Store
}

func (f *ksTradeFetcher) fetch(startTime time.Time, endTime time.Time, pageSize int, parameter any, resp any) (any, error) {
	cursor := getCursor(tradeCursor, resp)
	result, err := ksClient.Execute(context.Background(), "open.order.cursor.list", ks.Parameters{
		"orderViewStatus": 1,
		"pageSize":        pageSize,
		"beginTime":       startTime.UnixMilli(),
		"endTime":         endTime.UnixMilli(),
		"cpsType":         parameter,
		"cursor":          cursor,
	})
	if err != nil {
		return nil, err
	}
	return result, nil
}

func (f *ksTradeFetcher) hasNext(resp any) bool {
	return getCursor(tradeCursor, resp) != endCursor
}

func (f *ksTradeFetcher) buildDocument(resp any) []model.Document {
	result := resp.(gjson.Result)
	if result.Get("result").Int() != 1 {
		return nil
	}
	orderList := result.Get("result.data.orderList").Array()
	documents := make([]model.Document, len(orderList))
	for _, o := range orderList {
		info := o.Get("orderBaseInfo")
		var document model.Document
		document.Sn = info.Get("oid").String()
		document.Doc = o.String()
		documents = append(documents, document)
	}
	return documents
}

func (f *ksTradeFetcher) pageSize() int {
	return 50
}

func (f *ksTradeFetcher) parameters() []any {
	return []any{1}
}

func (f *ksRefundFetcher) hasNext(resp any) bool {
	return getCursor(refundCursor, resp) != endCursor
}

type ksRefundFetcher struct {
	store *model.Store
}

func (f *ksRefundFetcher) fetch(startTime time.Time, endTime time.Time, pageSize int, parameter any, resp any) (any, error) {
	cursor := getCursor(refundCursor, resp)
	result, err := ksClient.Execute(context.Background(), "open.seller.order.refund.pcursor.list", ks.Parameters{
		"type":      9,
		"pageSize":  pageSize,
		"beginTime": startTime.UnixMilli(),
		"endTime":   endTime.UnixMilli(),
		"cpsType":   parameter,
		"cursor":    cursor,
	})
	if err != nil {
		return nil, err
	}
	return result, nil
}

func getCursor(jsonpath string, resp any) string {
	if resp == nil {
		return ""
	}
	result := resp.(gjson.Result)
	return result.Get(jsonpath).String()
}

func (f *ksRefundFetcher) buildDocument(resp any) []model.Document {
	result := resp.(gjson.Result)
	if result.Get("result").Int() != 1 {
		return nil
	}
	orderList := result.Get("result.data.refundOrderInfoList").Array()
	documents := make([]model.Document, len(orderList))
	for _, o := range orderList {
		info := o.Get("orderBaseInfo")
		var document model.Document
		document.Sn = info.Get("oid").String()
		document.Rsn = info.Get("refundId").String()
		document.Doc = o.String()
		documents = append(documents, document)
	}
	return documents
}

func (f *ksRefundFetcher) pageSize() int {
	return 100
}

func (f *ksRefundFetcher) parameters() []any {
	return []any{1}
}
